package com.wing.light.exceltostf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.StringTokenizer;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ExcelToSTF2 {

	/**
	 * @param args
	 * @throws IOException
	 * @throws BiffException
	 */
	public static void main(String[] args) throws IOException, BiffException {
		String initPath = "C:" + File.separator + "initPath.txt";
		File initFile = new File(initPath);
		if (!initFile.exists()) {
			System.out.println("C盘根目录下没有initPath.txt文件，请确认。");
			return;
		}
		InputStream input = new FileInputStream(initPath);
		byte b[] = new byte[1024];
		input.read(b);
		input.close();
		StringTokenizer str = new StringTokenizer(new String(b));
		String iPath = str.nextToken();
		String oPath = str.nextToken();
		File inputPath = new File(iPath);
		File outputPath = new File(oPath);
		if (!inputPath.exists()) {
			System.out.println("输入文件夹" + iPath + "不存在，请检查。");
			return;
		}
		if (!outputPath.exists()) {
			System.out.println("输出文件夹" + oPath.trim() + "不存在，将会被创建。");
			outputPath.mkdir();
		}
		File[] files = inputPath.listFiles();
		for (int i = 0; i < files.length; i++) {
			// 遍历所有文件
			if (files[i].getName().endsWith(".xls")) {
				Workbook wb = Workbook.getWorkbook(files[i]);
				if (wb == null) {
					System.out.println(files[i].getName() + "文件为空。");
					return;
				}
				Sheet[] sheets = wb.getSheets();
				if (sheets != null && sheets.length > 0) {
					// 获得文件下所有表格
					for (int j = 0; j < sheets.length; j++) {
						if (sheets[j].getRows() > 1
								&& sheets[j].getColumns() > 0) {
							//表格不为空时，在output目录生成一个“文件名_表名.stf”文件
							String tPath = oPath.trim() + File.separator
									+ files[i].getName() + "_"
									+ sheets[j].getName() + ".stf";
							File tFile = new File(tPath);
							System.out.println("开始写入" + tPath);
							
							//写入stf文件
							Writer ow = new FileWriter(tFile);
							if(ow != null){
								ow.write(String.valueOf(sheets[j].getColumns()));
								ow.write("\n");
								Cell[] fCells = sheets[j].getRow(0);
								Cell[] sCells = sheets[j].getRow(1);
								for(int ci = 0; ci < sCells.length; ci++){
									ow.write(fCells[ci].getContents());
									ow.write("\t");
									if(sCells[ci].getType().toString().equalsIgnoreCase("number")){
										ow.write("real");
									}else if(sCells[ci].getType().toString().equalsIgnoreCase("label")){
										ow.write("String");
									}
									ow.write("\n");
								}
								for(int k = 1; k < sheets[j].getRows(); k++){
									Cell[] tCells = sheets[j].getRow(k);
									for(int ki = 0; ki < tCells.length; ki++){
										ow.write(tCells[ki].getContents());
										if(ki < tCells.length-1){
											ow.write("\t");
										}
									}
									if(k < sheets[j].getRows() -1){
										ow.write("\n");
									}
								}
								ow.close();
								System.out.println("写入" + tPath + "成功。\n");
							}
						}
					}
				}
			}
		}
	}
}
